package com.lzy.chat.c_service;

import com.lzy.chat.a_entity.User;
import com.lzy.chat.b_mapper.UserMapper;
import com.lzy.chat.c_service.Exception.Exceptions;
import com.lzy.chat.c_service.Exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;


import static com.lzy.chat.c_service.UserServiceImpl.getSHA256Password;


@Service("PasswordLogin")
public class PasswordLoginServiceImpl implements LoginService{
    private UserMapper userMapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public  User login(Integer id, String password){
        User user = userMapper.getUserById(id);
        if (user == null) throw new ServiceException(Exceptions.UserNotFoundException, "未找到此用户->id:" + id);
        String SHA256Password = user.getPassword();
        String salt = user.getSalt();
        String afterPassword = getSHA256Password(password, salt);
        if (!afterPassword.equals(SHA256Password)) throw new ServiceException(Exceptions.UserPasswordNotMatchException);
        return user;
    }


}
